﻿#include "mainscene.h"
#include "ui_mainscene.h"
#include<QDebug>
#include<QTimer>
#include<QMessageBox>
#include<QSqlDatabase>
#include<QSqlError>
#include<QSqlQuery>
#include<QSqlTableModel>
#include<QSqlRecord>
#include<QVariantList>

MainScene::MainScene(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainScene)
{
    ui->setupUi(this);

    //主界面窗口设置
    this->setFixedSize(698,496);
    this->setWindowTitle(QStringLiteral("数据库界面"));


    //数据库连接
    QSqlDatabase l_db = QSqlDatabase::addDatabase("QMYSQL");

    l_db.setHostName("localhost");
    l_db.setUserName("root");
    l_db.setPassword("123456");
    l_db.setDatabaseName("mate");

    if(!l_db.open())
    {
        QMessageBox::warning(this,"Error",l_db.lastError().text());
        return;
    }


    //Model显示数据库
    pg_Model = new QSqlTableModel;
    pg_Model->setTable("mate");
    ui->tableView->setModel(pg_Model);
    pg_Model->select();

    //设置手动提交
    pg_Model->setEditStrategy(QSqlTableModel::OnManualSubmit);

}

MainScene::~MainScene()
{
    delete ui;
}

/*************************************************

  Description:    返回主界面按钮槽函数
  return:         void
  Called By:
  Input:          无
  Auther:         林锦涛
  Time:           2021-1-23 14:31:48

*************************************************/
void MainScene::on_btnback_clicked()
{
    //发送返回信号
    emit this->backLogin();

}


/*************************************************

  Description:    添加数据按钮槽函数
  return:         void
  Called By:
  Input:          无
  Auther:         林锦涛
  Time:           2021-1-23 14:31:48

*************************************************/
void MainScene::on_btnadd_clicked()
{
    //创建空记录，获取行号，插入到表格中
    QSqlRecord l_Record = pg_Model->record();
    int l_iRow = pg_Model->rowCount();
    pg_Model->insertRecord(l_iRow,l_Record);

    ui->textEdit->append(QStringLiteral("增加人员成功!"));
}


/*************************************************

  Description:    删除数据按钮槽函数
  return:         void
  Called By:
  Input:          无
  Auther:         林锦涛
  Time:           2021-1-23 14:31:48

*************************************************/
void MainScene::on_btndelete_clicked()
{
    //获取选中区域
    QItemSelectionModel* l_Model = ui->tableView->selectionModel();
    QModelIndexList l_List =  l_Model->selectedRows();

    //错误提示
    int l_isOK = QMessageBox::warning(this, "Delete!", "Are you sure delete?",
                                      QMessageBox::Yes, QMessageBox::No);

    if (l_isOK == QMessageBox::No)
    {
        pg_Model->revertAll();
    }
    else
    {
        //循环删除选中行
        for (int i = 0; i < l_List.size(); i++)
        {
            pg_Model->removeRows(l_List.at(i).row(), 1);
        }
    }

    ui->textEdit->append(QStringLiteral("删除人员成功!"));
}


/*************************************************

  Description:    确定提交按钮槽函数
  return:         void
  Called By:
  Input:          无
  Auther:         林锦涛
  Time:           2021-1-23 14:31:48

*************************************************/
void MainScene::on_btnsure_clicked()
{
    //开始事务操作
    pg_Model->database().transaction();
    if(!pg_Model->submitAll())
    {
        pg_Model->database().rollback(); //回滚

        QString l_str = QString("数据库错误: %1").arg(pg_Model->lastError().text());
        QMessageBox::warning(this,"tableModel",l_str);
    }

    ui->textEdit->append(QStringLiteral("提交数据成功!"));
}


/*************************************************

  Description:    取消按钮槽函数
  return:         void
  Called By:
  Input:          无
  Auther:         林锦涛
  Time:           2021-1-23 14:31:48

*************************************************/
void MainScene::on_btncancel_clicked()
{
    pg_Model->revertAll();
    pg_Model->submitAll();

    ui->textEdit->append(QStringLiteral("操作已取消!"));
}


/************************************************
  Description:    查找按钮槽函数，由于技术限制目前
                  不能实现随机查找，只能按名字或者
                  说按照某一个关键字查找
  return:         void
  Called By:
  Input:          无
  Auther:         林锦涛
  Time:           2021-1-23 14:31:48

*************************************************/
void MainScene::on_btnfind_clicked()
{

    QString l_Name = ui->lineEdit->text();

    if (l_Name.length())
    {
        //安装过滤器过滤查找内容
        QString str = QString("name = '%1' ").arg(l_Name);
        pg_Model->setFilter(str);

        ui->textEdit->append(QStringLiteral("查找人员成功!"));
    }
    else
    {
        QMessageBox::warning(this,"Waring","Name is Empty!");
    }

}



/*************************************************

  Description:    查看全表按钮槽函数
  return:         void
  Called By:
  Input:          无
  Auther:         林锦涛
  Time:           2021-1-23 14:31:48

*************************************************/
void MainScene::on_btnseeall_clicked()
{
    pg_Model->setTable("mate");
    pg_Model->select();

    ui->textEdit->append(QStringLiteral("成功显示全表!"));
}

/*************************************************

  Description:    清除状态框按钮槽函数
  return:         void
  Called By:
  Input:          无
  Auther:         林锦涛
  Time:           2021-1-23 14:31:48

*************************************************/
void MainScene::on_btnstatus_clicked()
{
    ui->textEdit->clear();
}
